package com.gmgx.hrms.dao.impl;

import com.gmgx.hrms.dao.AttDao;
import com.gmgx.hrms.entity.Att;
import com.gmgx.hrms.util.JDBCTools;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;

public class AttDaoImpl  implements AttDao {

    private JDBCTools tools=new JDBCTools();
    @Override
    public boolean addAtt(Att att) {
       int i=  tools.update("insert into t_att (att_emp,att_start,att_end) values(?,?,?)",att.getAtt_emp().getEmp_id(),att.getAtt_start(),att.getAtt_end());
        return i>0;
    }

    @Override
    public boolean updateAtt(Att att) {
       int i= tools.update("update t_att set att_end=? where att_emp=? and date(att_start)=?",att.getAtt_end(),att.getAtt_emp().getEmp_id(),LocalDate.now());
        return i>0;
    }

    @Override
    public boolean queryAttByEmpIdAndNow(Integer empid) {
        ResultSet rs=tools.query("select * from t_att where att_emp=? and (date(att_start)=? or date(att_end)=?)",empid, LocalDate.now(),LocalDate.now());
        try {
            if(rs.next()){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return false;
    }
}
